using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.Drawing;
using System.IO;
using System.Threading;
using ViewOnCore;
using ViewOnCore.HostContract;
using ViewOnCore.Visual;

namespace ViewOn.AddInsHostContract.Visual
{

    /// <summary>
    /// Concrete implementation of the visual service.
    /// </summary>
    sealed public class VisualService : IVisualService
    {

        #region Variables

        private VisualPartMode _addInViewMode = VisualPartMode.Normal;

        private List<VisualPart> _visualParts = new List<VisualPart>();

        public event VisualPartModeChangedEvent VisualPartModeChanged;

        public event EventHandler NavigateToRequest;
        public event EventHandler NavigateBackRequest;

        #endregion

        #region Properties

        public VisualPartMode VisualPartMode
        {
            get
            {
                return _addInViewMode;
            }
            set
            {
                VisualPartMode previousMode = _addInViewMode;
                _addInViewMode = value;

                if (VisualPartModeChanged != null)
                    VisualPartModeChanged(previousMode, value);
            }
        }

        public ReadOnlyCollection<VisualPart> VisualParts
        {
            get
            {
                return _visualParts.AsReadOnly();
            }
        }

        #endregion

        #region AddVisualPart

        public void AddVisualPart(VisualPart visualPart)
        {
            _visualParts.Add(visualPart);
        }

        #endregion

        #region RemoveVisualPart

        public void RemoveVisualPart(VisualPart visualPart)
        {
            _visualParts.Remove(visualPart);
        }

        #endregion

        #region UnactiveVisualPart

        public void UnactiveVisualPart()
        {
            foreach (VisualPart visualAddIn in _visualParts)
                if (visualAddIn.IsActive)
                    visualAddIn.IsActive = false;
        }

        #endregion

        #region NavigateTo

        public void NavigateTo(string visualPartHandle)
        {
            if (NavigateToRequest != null)
                NavigateToRequest(visualPartHandle, null);
        }

        #endregion

        #region NavigateBack

        public void NavigateBack()
        {
            if (NavigateBackRequest != null)
                NavigateBackRequest(null, null);
        }

        #endregion

    }
}
